package com.ifast.asset.controller;


import java.util.Arrays;

import com.ifast.asset.domain.AssetAndGroup;
import com.ifast.asset.domain.AssetAndGroupDO;
import com.ifast.asset.service.AssetAndGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-08-05 13:48:16 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/assetAndGroup")
public class AssetAndGroupController extends BaseController {
	@Autowired
	private AssetAndGroupService assetAndGroupService;
	
	@GetMapping()
	@RequiresPermissions("wxmp:assetAndGroup:assetAndGroup")
	String AssetAndGroup(){
	    return "wxmp/assetAndGroup/assetAndGroup";
	}
	
	@ResponseBody
	//@GetMapping("/list")
	//@RequiresPermissions("wxmp:assetAndGroup:assetAndGroup")
	public Result<Page<AssetAndGroupDO>> list(AssetAndGroup assetAndGroup){
		AssetAndGroupDO assetAndGroupDTO = assetAndGroup.getAssetAndGroupDO();
        Wrapper<AssetAndGroupDO> wrapper = new EntityWrapper<>(assetAndGroupDTO).orderBy("id", false);
        Page<AssetAndGroupDO> page = assetAndGroupService.selectPage(getPage(AssetAndGroupDO.class,assetAndGroup.getPageNum(),assetAndGroup.getPageSize()), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wxmp:assetAndGroup:add")
	String add(){
	    return "wxmp/assetAndGroup/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wxmp:assetAndGroup:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		AssetAndGroupDO assetAndGroup = assetAndGroupService.selectById(id);
		model.addAttribute("assetAndGroup", assetAndGroup);
	    return "wxmp/assetAndGroup/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("wxmp:assetAndGroup:add")
	public Result<String> save(@RequestBody AssetAndGroup assetAndGroup){
		for(int i = 0 ; i < assetAndGroup.getAssetIds().length ; i++){

			AssetAndGroupDO assetAndGroupDo = assetAndGroup.getAssetAndGroupDO();
			assetAndGroupDo.setAssetId(assetAndGroup.getAssetIds()[i]);
			assetAndGroupService.insert(assetAndGroupDo);
		}
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("wxmp:assetAndGroup:edit")
	public Result<String>  update(@RequestBody AssetAndGroupDO assetAndGroup){
		boolean update = assetAndGroupService.updateById(assetAndGroup);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("wxmp:assetAndGroup:remove")
	public Result<String>  remove( Integer id){
		assetAndGroupService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("wxmp:assetAndGroup:batchRemove")
	public Result<String>  remove(@RequestBody AssetAndGroup assetAndGroup){
		for (int i = 0 ; i < assetAndGroup.getAssetIds().length ; i++){

			Wrapper wrapper = new EntityWrapper();
			wrapper.eq("assetId",assetAndGroup.getAssetIds()[i]);
			wrapper.eq("groupId",assetAndGroup.getGroupId());
			assetAndGroupService.delete(wrapper);
		}
		return Result.ok();
	}
	
}
